# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# pyojo                                           Copyright (c) 2011 nabla.net
# ----------------------------------------------------------------------------
""" Contains “form widgets” to allow the user to type or select a value..

The dijit.form widgets include:

    - **Buttons**: Button, DropDownButton, ComboButton, ToggleButton.

    - **Input**: TextBox, NumberTextBox, NumberSpinner, CurrencyTextBox,
      DateTextBox, TimeTextBox, ValidationTextBox.
      
    - **TextArea**: SimpleTextarea, Textarea.

    - **Select**: Select, ComboBox, MultiSelect, FilteringSelect.

    - **Miscellaneous**: CheckBox, RadioButton, HorizontalSlider,
      VerticalSlider, DataList.

"""
from pyojo.js.code import Code
from pyojo.js.dojo._base import Dojo
from . import Dijit



class Form(Dijit):
    """ Group of data input widgets.
    
        Provides various methods for serializing/deserializing form data, and 
        checking whether or not a form is valid
    """
    
    require = ["dojo/ready","dijit/form/Form"]
    events = ["reset", "submit"]
    
    

#----------------------------------------------------------------------------
#--- Buttons

class Button(Dijit):
    """ A representation of a normal button.
    
        @param label: Label
        @param onClick: function
    
    """
    startup = False
    require = ["dojo/ready", "dijit/form/Button", "dojo/domReady!"]
    


class DropDownButton(Dijit):
    """ A button which displays a menu or some other popup.
    """
    require = ["dojo/ready","dijit/form/DropDownButton"]
    


class ComboButton(Dijit):
    """  A button with an arrow to show a drop down.
    """
    require = ["dojo/ready","dijit/form/ComboButton"]



class ToggleButton(Dijit):
    """  A state button.
    """
    require = ["dojo/ready","dijit/form/ToggleButton"]
    
    
#----------------------------------------------------------------------------
#--- Input

class TextBox(Dijit):
    """  A basic text input control.
    
    You can use type="hidden" to have hidden inputs compatible with 
    dijit.form.Form.
    """
    require = ["dojo/ready","dijit/form/TextBox"]
    

    
class NumberTextBox(Dijit):
    """  A input widget which restricts input to numeric values.
    """
    require = ["dojo/ready","dijit/form/NumberTextBox"]



class NumberSpinner(Dijit):
    """  An numeric values input widget with  down and up arrow buttons.
    """
    require = ["dojo/ready","dijit/form/NumberSpinner"]



class CurrencyTextBox(Dijit):
    """ A specialized input for monetary values.
    """
    require = ["dojo/ready","dijit/form/CurrencyTextBox"]



class DateTextBox(Dijit):
    """  A date entry control.
    """
    require = ["dojo/ready","dijit/form/DateTextBox"]



class TimeTextBox(Dijit):
    """  A time entry control.
    """
    require = ["dojo/ready","dijit/form/TimeTextBox"]


    
class ValidationTextBox(Dijit):
    """ Checks to make sure the user’s input is valid 
    
    :param required: Whether the field is required or not. false by default.
    :param promptMessage: Tooltip text that appears when the text box is empty.
    :param invalidMessage: Tooltip text if the text box is invalid.
    :param missingMessage: Tooltip text if the field is required and is empty.
    :param constraints: Holds constraints for use in subclasses.
    :param regExp: Regular expression pattern to be used for validation. 
        If this is used, do not use regExpGen.
    :param regExpGen: Function that should return a regular expression; 
        see the String.replace() format. If this is used, do not use regExp.
    :param tooltipPosition: Where Tooltip will appear.
    :param sValid: Method that calls validator function.
    :param validator: Method that determines whether the content of the text 
        box is valid. Called at these events: onblur, oninit, onkeypress.
    :param displayMessage: Method that displays validation errors.
    """
    require = ["dojo/ready","dijit/form/ValidationTextBox"]



#----------------------------------------------------------------------------
#--- Text

    
class SimpleTextarea(Dijit):
    """  A simple text area.
    """
    require = ["dojo/ready", "dijit/form/SimpleTextarea"]
    
    def X__init__(self, var, target): 
        
        self.loc = "var %s = new SimpleTextarea({rows:4}, '%s');" % (var, 
                                                                     target)
        
    def Xinit(self):
        self.para.update({"name":self.var, "options":Code(sss)})
        
        
class Textarea(Dijit):
    """  An auto expanding/contracting text area.
    """
    require = ["dojo/ready","dijit/form/Textarea"]
    
    
#----------------------------------------------------------------------------
#--- Select

sss = """[      { label: "Español", value: "ES", selected: true },
                { label: "Inglés", value: "EN" },
                { label: "Alemán", value: "GR" },
                { label: "Francés", value: "FR" },
                { label: "Chino", value: "CH" }
      ]"""
                
class Select(Dijit):
    """ A styleable drop down select box.
    """
    require = ["dojo/ready","dijit/form/Select"]



class ComboBox(Dijit):
    """ Allows to type any value and just gives suggested completions.
    """
    require = ["dojo/ready","dijit/form/ComboBox"]



class MultiSelect(Dijit):
    """ Allows the selection of multiple items.
    """
    require = ["dojo/ready","dijit/form/MultiSelect"]



class FilteringSelect(Dijit):
    """ A Select with type-ahead filtering of the drop down list.
    """
    require = ["dojo/ready","dijit/form/FilteringSelect"]


      
#----------------------------------------------------------------------------
#--- Miscellaneous


class CheckBox(Dijit):
    """ A checkbox with fanxcy styling.
    """
    require = ["dojo/ready", "dijit/form/CheckBox"]


class RadioButton(Dijit):
    """ A Select with type-ahead filtering of the drop down list.
    """
    require = ["dojo/ready","dijit/form/RadioButton"]


class HorizontalSlider(Dijit):
    """ A scale with a handle you can drag left/right to select a value.
    """
    require = ["dojo/ready","dijit/form/HorizontalSlider"]
    

class VerticalSlider(Dijit):
    """ A scale with a handle you can drag up/down to select a value.
    """
    require = ["dojo/ready","dijit/form/VerticalSlider"]


class DataList(Dijit):
    """ Data store that processes OPTION tags as data items.
    """
    require = ["dojo/ready","dijit/form/DataList"]





